' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2023.09.17.17.11]) on 2023.10.04 at 01:27 (Coordinated Universal Time)
'============
'BALLRAIN.BAS
'============
'Balls rain from the top, some drop, some bounce then sink away.
'For QB64 by Dav, SEP/2023
'Ported to BASIC Anywhere Machine by Charlie Veniot
 
SCREEN _NEWIMAGE(1000, 600, 32)
 
balls = 200 'number of balls on screen, 200 is my laptop's comfort zone
 
DIM ballx(balls), bally(balls), ballxvel(balls), ballyvel(balls), ballsize(balls)
DIM ballred(balls), ballgrn(balls), ballblu(balls)
 
🔶NewIteration: 

'make random ball values
FOR b = 1 TO balls
    ballx(b) = RND * (_WIDTH) 'x position
    bally(b) = RND * -(_HEIGHT) 'y position
    ballxvel(b) = INT(RND * 7) 'x speed
    ballyvel(b) = INT(RND * 3) 'y speed
    ballsize(b) = RND * 15 + 10 'ball size
    ballred(b) = RND * 255 'red color
    ballgrn(b) = RND * 255 'green color
    ballblu(b) = RND * 255 'blue color
NEXT
 
DO
    CLS
 
    FOR b = 1 TO balls
 
        IF bally(b) < _HEIGHT - ballsize(b) THEN
 
            ballx(b) = ballx(b) + ballxvel(b)
            bally(b) = bally(b) + ballyvel(b)
 
            IF ballx(b) < ballsize(b) OR ballx(b) > _WIDTH - ballsize(b) THEN
                ballxvel(b) = -ballxvel(b)
            END IF
 
            IF bally(b) < ballsize(b) OR bally(b) > _HEIGHT - (ballsize(b) * 2) THEN
                ballyvel(b) = -ballyvel(b)
            END IF
 
            ballyvel(b) = ballyvel(b) + 3 'gravity value
            'ballxvel(b) = ballxvel(b) + (Rnd * 1.2 - Rnd * 1.2) 'x shake
 
            'draw gradient ball
            FOR y2 = bally(b) - ballsize(b) TO bally(b) + ballsize(b)
                FOR x2 = ballx(b) - ballsize(b) TO ballx(b) + ballsize(b)
                    clr = (ballsize(b) - (SQR((x2 - ballx(b)) * (x2 - ballx(b)) + (y2 - bally(b)) * (y2 - bally(b))))) / ballsize(b)
                    IF clr > 0 THEN PSET (x2, y2), _RGB(clr * ballred(b), clr * ballgrn(b), clr * ballblu(b))
                NEXT
            NEXT
 
        END IF
    NEXT
 
    'see if balls done
    onscreen = 0
    FOR b = 1 TO balls
        IF bally(b) < _HEIGHT - ballsize(b) THEN onscreen = 1
    NEXT
    _DISPLAY 
LOOP UNTIL onscreen = 0

GOTO 🔶NewIteration